<?php
/**
 *  💡 免责声明：本软件不得用于商业用途，仅做学习交流
 *  ⚠️ 权利声明：本软件由版权人提供产品技术支持
 *  📅 项目编号：NX2025-0135
 */
namespace App\Admin\Controllers;

use App\Admin\Repositories\GroupOrder;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Http\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;

class GroupOrderController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new GroupOrder(['user', 'product', 'session']), function (Grid $grid) {
            $grid->disableCreateButton();
            $grid->disableRefreshButton();
            $grid->disableViewButton();
            $grid->disableRowSelector();
            $grid->disableActions();
            $grid->model()->orderByDesc('id');
            $grid->export()->xlsx()->rows(function ($rows) {
                return $this->dataProcessing($rows);
            });

            $grid->quickSearch('user.telephone')->placeholder('搜索电话');
            $grid->selector(function (Grid\Tools\Selector $selector) {
                $selector->select('status', '拼团状态', [
                    1 => '拼团中',
                    2 => '已退款',
                    3 => '已发券',
//                    4 => '已发货',
                ]);
            });
            $grid->column('id')->sortable();
            $grid->column('user.telephone', '会员电话')->copyable();
            $grid->column('session.name', '场次名称');
            $grid->column('session.date', '场次日期');
//            $grid->column('product.name', '商品名称');
//            $grid->column('product.main_image', '商品图片')->image('', 50, 50);
//            $grid->column('order_no');
            $grid->column('number');
            $grid->column('amount');
//            $grid->column('method', '配送方式')->using([
//                1 => '快递配送',
//                2 => '同城配送',
//            ]);
//            $grid->column('gold_amount', 'KC通证数量');
            $grid->column('bean_burn', '失败燃烧数');
            $grid->column('bean_unit_price', 'KC通证单价');
            $grid->rows(function (Collection $rows) {
                $rows->each(function ($item) {
                    if ($item->is_winner == '已中单') {
                        $item->setAttributes(['class' => 'bg-success']);
                    }
                });
            });
            $grid->column('is_winner')->using([
                0 => '未中单',
                1 => '已中单',
            ]);
            $grid->column('is_card', '使用垫付卡')->using([
                0 => '未使用',
                1 => '已使用',
            ]);
//            $grid->column('card_id');
            $grid->column('is_forced')->using([
                0 => '否',
                1 => '是',
            ]);
            $grid->column('user_order_count');
            $grid->column('status', '拼团状态')->using([
                1 => '拼团中',
                2 => '已退款',
                3 => '已发券',
//                3 => '待发货',
//                4 => '已发货',
            ]);
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                $filter->equal('id');
                $filter->equal('user.telephone', '用户电话');
                $filter->like('order_no');
//                $filter->equal('method')->select([
//                    1 => '快递发货',
//                    2 => '同城配送',
//                ]);
                $filter->equal('is_winner', '是否中单')->select([
                    0 => '未中单',
                    1 => '已中单',
                ]);
                $filter->equal('status', '拼单状态')->select([
                    1 => '拼团中',
                    2 => '已退款',
                    3 => '已发券',
//                    3 => '待发货',
//                    4 => '已发货',
                ]);
                $filter->date('session.date', '场次日期');
                $filter->equal('session.name', '场次名称');
                $filter->equal('product.name', '商品名称');
                $filter->equal('is_forced', '强制中单')->select([
                    0 => '否',
                    1 => '是',
                ]);
                $filter->equal('is_card', '使用垫付卡')->select([
                    0 => '否',
                    1 => '是',
                ]);
                $filter->between('created_at', '创建时间')->datetime();
                $filter->between('updated_at', '修改时间')->datetime();
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new GroupOrder(), function (Show $show) {
            $show->field('id');
            $show->field('user_id');
            $show->field('session_id');
            $show->field('product_id');
            $show->field('order_no');
            $show->field('amount');
            $show->field('gold_amount');
            $show->field('number');
            $show->field('is_winner');
            $show->field('is_card');
            $show->field('card_id');
            $show->field('is_forced');
            $show->field('user_order_count');
            $show->field('status');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new GroupOrder(), function (Form $form) {
            $form->display('id');
            $form->text('user_id');
            $form->text('session_id');
            $form->text('product_id');
            $form->text('order_no');
            $form->text('amount');
            $form->text('gold_amount');
            $form->text('number');
            $form->text('is_winner');
            $form->text('is_card');
            $form->text('card_id');
            $form->text('is_forced');
            $form->text('user_order_count');
            $form->text('status');

            $form->display('created_at');
            $form->display('updated_at');
        });
    }

    protected function dataProcessing($rows)
    {
        foreach ($rows as $index => &$row) {
            $row['is_winner'] = Arr::get([
                0 => '未中单',
                1 => '已中单',
            ], $row['is_winner'], $row['is_winner']);

            $row['is_card'] = Arr::get([
                0 => '未使用',
                1 => '已使用',
            ], $row['is_card'], $row['is_card']);

            $row['is_forced'] = Arr::get([
                0 => '否',
                1 => '是',
            ], $row['is_forced'], $row['is_forced']);

            $row['status'] = Arr::get([
                1 => '拼团中',
                2 => '已退款',
                3 => '待发货',
                4 => '已发货',
            ], $row['status'], $row['status']);
        }
        return $rows;
    }
}
